package studentmanage.service.impl;

import studentmanage.DTO.AdminDTO;
import studentmanage.VO.AdminVO;
import studentmanage.service.AdminService;
import studentmanage.tools.properties.JwtProperties;
import studentmanage.tools.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j

public class AdminServiceImpl implements AdminService {
    public static String ADMIN_ID="admin";
    public static String PASSWORD="123456";

    @Autowired
    JwtProperties jwtProperties;
    public AdminVO login(AdminDTO adminDTO){
        if (!adminDTO.getAdminId().equals(ADMIN_ID) || !adminDTO.getPassword().equals(PASSWORD)){
            log.info("管理员登录失败");
            return null;
        }

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("admin", ADMIN_ID);
        //生成jwt令牌
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),   //yml中的秘钥student_manager
                jwtProperties.getAdminTtl(),       //过期时间
                claims);

        AdminVO adminVO = AdminVO.builder()
                .adminId(adminDTO.getAdminId())
                .token(token)
                .build();
        return adminVO;
    }
}
